<?php
/**
 * 后台主界面
 * @Author 聂哥<376927050@qq.com>
 * @Date: 2022-10-11 19:50:59
*/
namespace app\admin\controller\system;

use app\admin\exception\ParameterException;
use app\admin\model\system\SystemModule;
use app\admin\model\system\SystemMenu as MenuModel;
use app\admin\service\MenuService;
use app\admin\validate\system\Menu as MenuValidate;
use Exception;

class Menu extends Base
{
    /**
     * 管理员
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = MenuModel::order('id','desc')->select();
            return [
                'code' => 0,
                'msg' => '',
                'count' => $list->count(),
                'data' => $list->toArray()
            ];
        }
        return $this->fetch();
    }

   /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //验证
            $validate = new MenuValidate;
            if(!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            
            $menu = new MenuModel();
            if($menu->save($data) == false){
                throw new Exception('添加失败');
            }
            MenuService::reset();
            return ['code'=>200,'msg'=>'添加成功'];
        }
        return $this->fetch('', [
            'permissions' => get_tree(MenuModel::order('sort','desc')->select()->toArray()),
            'modules' => SystemModule::order(['name'])->column('name', 'id'),
        ]);
    }

     /**
     * 编辑菜单
     */
    public function edit($id)
    { 
        $menu = MenuModel::find($id);
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //验证
            $validate = new MenuValidate;
            if(!$validate->scene('edit')->check($data)) {
                throw new Exception($validate->getError());
            }
            if( $menu->save($data) == false ){
                throw new Exception('编辑菜单失败');
            }
            MenuService::reset();
            return [
                'code' => 200,
                'msg'  => '编辑菜单成功'
            ];
        }
        $menuData = MenuModel::order('sort','desc')->select();
        return $this->fetch('',[
            'model' => $menu,
            'menus' => get_tree($menuData),
            'modules' => SystemModule::order(['name'])->column('name', 'id'),
        ]);
    }

    /**
     * 禁用，启用
     */
    public function status()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');
        if (!in_array($status,[1,2])){
            throw new ParameterException('参数不在范围');
        }
        $menu =  MenuModel::find($id);
        if ($menu->isEmpty()){
            throw new ParameterException('无效的id');
        }
        $menu->status = $status;
        if( $menu->save() == false ){
            throw new Exception('修改状态失败');
        }
        MenuService::reset();
        return [
            'code' => 200,
            'msg'  => '修改状态成功'
        ];
    }

    /**
     * 删除
     */
    public function del($id)
    {
        $menu =  MenuModel::with('child')->find($id);
        if ($menu->isEmpty()){
            throw new ParameterException('无效的id');
        }
        if (isset($menu->child) && !$menu->child->isEmpty()){
            throw new ParameterException('存在子权限，禁止删除');
        }
        if( $menu->delete() == false ){
            throw new Exception('删除菜单失败');
        }
        MenuService::reset();
        return [
            'code' => 200,
            'msg'  => '删除菜单成功'
        ];
    }
  
}
